#include <iostream>
using namespace std;
#include <iostream>
#include <string>
#include <sstream>

int getCardValue(const std::string& card) {
    if (card == "3") return 1;
    if (card == "4") return 2;
    if (card == "5") return 3;
    if (card == "6") return 4;
    if (card == "7") return 5;
    if (card == "8") return 6;
    if (card == "9") return 7;
    if (card == "10") return 8;
    if (card == "J") return 9;
    if (card == "Q") return 10;
    if (card == "K") return 11;
    if (card == "A") return 12;
    if (card == "2") return 13;
    if (card == "joker") return 14;
    if (card == "JOKER") return 15;
    return 0;
}

std::string compareCards(const std::string& hand1, const std::string& hand2) {
    std::istringstream iss1(hand1);
    std::istringstream iss2(hand2);
    std::string card1, card2;
    iss1 >> card1;
    iss2 >> card2;

    int value1 = getCardValue(card1);
    int value2 = getCardValue(card2);

    if (value1 == 0 || value2 == 0) {
        return "ERROR";
    }

    if (value1 == 14 && value2 == 15) {
        return hand2;
    }
    if (value1 == 15 && value2 == 14) {
        return hand1;
    }

    if (hand1 == "joker JOKER" || hand2 == "joker JOKER") {
        return "joker JOKER";
    }

    if (hand1 == hand2) {
        return hand1;
    }

    if (hand1.find(" ") != std::string::npos && hand2.find(" ") != std::string::npos) {
        int count1 = 1, count2 = 1;
        while (iss1 >> card1) {
            count1++;
        }
        while (iss2 >> card2) {
            count2++;
        }
        if (count1 == count2) {
            if (value1 > value2) {
                return hand1;
            }
            else {
                return hand2;
            }
        }
        else {
            return "ERROR";
        }
    }
    else {
        return "ERROR";
    }
}

//int main() {
//    std::string hand1, hand2;
//    std::cin >> hand1 >> hand2;
//
//    std::string result = compareCards(hand1, hand2);
//    std::cout << result << std::endl;
//
//    return 0;
//}